<?php

namespace App\Controllers\Diary;

use App\Models\Diary\Topic;
use App\Models\Diary\Diary;

class GetNewlestDiary extends \App\Libs\Base\BaseAction
{

    protected function beforeRun()
    {
        parent::beforeRun();
        //$this->needLogin = TRUE;
    }

    /**
     * @return array 返回开启显示的最新一期的5条日记
     */
    public function execute()
    {
        //获取最新的期数的数值
        $topicModel = new Topic();
        $period = $topicModel->getNewlestPeriodNum();
        if (empty($period)) {
            return \App\Libs\Views\Json::view(1, '没有开启显示的活动', null);
        }
        //获得这期最新的5条日记；
        $newlestDiary = Diary::findAll([
            'conditions' => "period = ?1",
            'columns' => 'id,diary_name,diary_logo,diary_num,period',
            'limit' => '3',
            'order' => 'ctime desc',
            'bind' => array(1 => $period)
        ])->toArray();
        if (empty($newlestDiary)) {
            return \App\Libs\Views\Json::view(1, '开启显示的最新一期没有日记', null);
        } else {
            return \App\Libs\Views\Json::view(0, '成功', $newlestDiary);
        }

    }
}
